package my.mall.admin.api.controller

import jakarta.validation.Valid
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import my.mall.admin.api.config.PreAuthorize
import my.mall.admin.api.pojo.login.LoginInfo
import my.mall.admin.api.pojo.media.MediaListParam
import my.mall.admin.api.pojo.media.MediaSaveParam
import my.mall.admin.api.service.MediaService
import my.mall.common.annotation.Login
import my.mall.common.pojo.CommonResp
import my.mall.common.pojo.DeleteParam
import my.mall.common.pojo.PageParam
import my.mall.common.pojo.PageResp
import my.mall.common.util.RespUtils
import my.mall.db.entity.Media
import my.mall.db.entity.MediaGroup
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("media")
class MediaController(
    private val mediaService: MediaService
) {

    @PreAuthorize(*["Media-Manage", "Media-List"])
    @GetMapping("list")
    fun getMediaList(
        @Login loginInfo: LoginInfo,
        param: @Valid MediaListParam,
        page: PageParam
    ): CommonResp<PageResp<Media>> {
        return mediaService.getMediaList(loginInfo, param, page)
    }

    @PreAuthorize("Media-Manage")
    @PostMapping("save")
    fun saveMedia(@Login loginInfo: LoginInfo, @RequestBody param: MediaSaveParam): CommonResp<*> {
        mediaService.saveMedia(loginInfo, param)
        return RespUtils.success()
    }

    @PreAuthorize("Media-Manage")
    @PostMapping("delete")
    fun deleteMedia(
        @Login loginInfo: LoginInfo,
        @RequestBody ids: @NotNull @Size(min = 1, max = 20) MutableList<Long?>?
    ): CommonResp<*> {
        mediaService.deleteMedia(loginInfo, ids)
        return RespUtils.success()
    }

    @PreAuthorize("Media-Manage")
    @PostMapping("edit")
    fun editMedia(@Login loginInfo: LoginInfo, @RequestBody media: Media): CommonResp<*> {
        mediaService.updateById(loginInfo, media)
        return RespUtils.success()
    }

    @PreAuthorize(*["Media-Manage", "Media-List"])
    @GetMapping("group/list")
    fun getMediaGroupList(@Login loginInfo: LoginInfo): CommonResp<List<MediaGroup?>?> {
        return RespUtils.success(mediaService.getMediaGroupList(loginInfo.storeId))
    }

    @PreAuthorize("Media-Manage")
    @PostMapping("group/saveOrEdit")
    fun saveOrEditGroup(@Login loginInfo: LoginInfo, @RequestBody mediaGroup: MediaGroup): CommonResp<*> {
        mediaService.saveOrEditGroup(loginInfo, mediaGroup)
        return RespUtils.success()
    }

    @PreAuthorize("Media-Manage")
    @PostMapping("group/delete")
    fun deleteGroup(@Login loginInfo: LoginInfo, @RequestBody param: DeleteParam): CommonResp<*> {
        mediaService.deleteMediaGroup(loginInfo, param)
        return RespUtils.success()
    }

}
